#include "stdafx.h"
#include "Module.h"
#include "System.h"

namespace ham
{
	Module::Module(QWidget* parent)
		: QMainWindow(parent), m_system(0),
		m_isInit(false)
	{
	}

	Module::~Module(void)
	{
	}
	
	bool Module::init(System* sys)
	{
		if (!sys)
			return false;

		m_system = sys;

		// has it already been init?
		if (m_isInit)
			return false;

		// load properties from file
		loadProperties();

		//LOG << "initializing module: " << getModuleName();

		// init module
		if (iInit())
			m_isInit = true;

		// was it successful?
		return m_isInit;
	}
	
	bool Module::isInit() const
	{
		return m_isInit && m_system;
	}
	
	System* Module::getSystem()
	{
		return m_system;
	}
	
	QList<QMenu*> Module::getMenus(QWidget* parent) const
	{
		// no menus by default
		return QList<QMenu*>();
	}

	QList<QPair<QString, QWidget*>> Module::getMainWidgets(QWidget* parent)
	{
		// no main widgets by default
		return QList<QPair<QString, QWidget*>>();
	}
	
	QList<QPair<QString, QWidget*>> Module::getToolbarWidgets(QWidget* parent)
	{
		// no toolbar widgets by default
		return QList<QPair<QString, QWidget*>>();
	}
	
	WdgOptions* Module::getOptionsWdg(QWidget* parent)
	{
		// no options widget by default
		return 0;
	}
	
	bool Module::loadProperties()
	{
		QString filename = getModuleName().toLower() + ".xml";
		return m_properties.loadFromFile(filename, getModuleName());
	}

	bool Module::saveProperties()
	{
		QString filename = getModuleName().toLower() + ".xml";
		return m_properties.saveToFile(filename, getModuleName());
	}
}